/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Semester.java
 *
 * Created on May 4, 2011, 4:27:18 PM
 */

package c0907m.group2.aitsm.gui;

import c0907m.group2.aitsm.core.db.ClassDb;
import c0907m.group2.aitsm.core.db.SemesterDb;
import c0907m.group2.aitsm.core.entity.ClassInfo;
import c0907m.group2.aitsm.core.entity.SemesterInfo;
import c0907m.group2.aitsm.gui.component.DefaultPanel;
import c0907m.group2.aitsm.gui.component.StatusBar;
import c0907m.group2.aitsm.gui.handler.TableCellListener;
import c0907m.group2.aitsm.util.Helper;
import c0907m.group2.aitsm.util.Settings;
import com.standbysoft.component.date.swing.JDatePicker;
import com.standbysoft.component.date.swing.plaf.basic.SpinnerTimePickerUI;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import java.awt.event.ActionEvent;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author ISTC
 */
public class Semester extends DefaultPanel {
    //default state on status bar when init
    public static final String DEFAULT_STATUS = "Add, update, delete semesters";
    public static final String DATE_FORMAT = "MMM dd, yyyy";
    public final Logger logger = Logger.getLogger(Semester.class.getName());
    private DefaultTableModel dtm;
    private StatusBar statusBar;
    private List<SemesterInfo> editingList;
    private List<ClassInfo> allClasses;
    private List<Integer> classIDs;
    private SemesterDb semesterDB;
    private DefaultCellEditor classEditor;
    private JComboBox cboClassTable;

    public void setStatusBar(StatusBar sb) {
        this.statusBar = sb;
    }

    /**
     * Creates new form Semester
     */
    public Semester() {
        initComponents();
        btnUpdate.setEnabled(false);
        statusBar = new StatusBar();
        semesterDB = new SemesterDb();
        editingList = new ArrayList<SemesterInfo>();
        dtm = (DefaultTableModel) tblSemesterList.getModel();
        cboClassTable = new JComboBox();
        loadDataToComboBox();
        loadSemestersToTable();
        tblSemesterListEventHandler();
    }


    /**
     * Reload all data from database
     */
    @Override
    public void refresh() {
        dtm.setRowCount(0);
        doReset();
        loadSemestersToTable();
    }

    /**
     * Update changes
     */
    @Override
    public void doUpdate() {
        if (editingList.size() == 0) {
            statusBar.setMessage("No changes have been made: Update cancelled.", StatusBar.WARNING_TYPE);
            return;
        }

        try {
            semesterDB.update(editingList);
            statusBar.setMessage("Update successful.", StatusBar.MESSAGE_TYPE);
        } catch (SQLException e) {
            logger.log(Level.WARNING, "Update failed: " + e.getMessage());
            statusBar.setMessage("Update failed.", StatusBar.ERROR_TYPE);
            refresh();
        }

        editingList.removeAll(editingList);
    }

    /**
     * Add new Semester to database
     */
    @Override
    public void doAdd() {
        String name = txtName.getText().trim();
        String startDate = dpStart.getText();
        String endDate = dpEnd.getText();

        if (name.equals("")) {

            JOptionPane.showMessageDialog(
                    this,
                    "Please fill all required fields",
                    "Message",
                    JOptionPane.ERROR_MESSAGE
            );

        } else {
            SemesterInfo newSemester = new SemesterInfo();


            long startTime = Helper.toDate(startDate, DATE_FORMAT).getTime();
            long endTime = Helper.toDate(endDate, DATE_FORMAT).getTime();
            int classId = allClasses
                    .get(cboClass.getSelectedIndex())
                    .getId();

            newSemester.setName(txtName.getText().trim());
            newSemester.setStartDate(new java.sql.Date(startTime));
            newSemester.setEndDate(new java.sql.Date(endTime));
            newSemester.setClassId(classId);
            newSemester.setDescription(txaDescription.getText().trim());
            newSemester.setActive(true);

            try {
                semesterDB.insert(newSemester);
                statusBar.setMessage("Semester added", StatusBar.MESSAGE_TYPE);
//                dtm.addRow(new Object[] {newSemester.getId(),
//                                    newSemester.getName(),
//                                    newSemester.getDescription(),
//                                    newSemester.isActive()});
                refresh();
            } catch (SQLException ex) {
                logger.log(Level.SEVERE, ex.getMessage());
                statusBar.setMessage("Adding failed.", StatusBar.ERROR_TYPE);
            }

        }
    }

    /**
     * Reset all fields to blank
     */
    @Override
    public void doReset() {
        txtName.setText("");
        txaDescription.setText("");
        txtSearchByName.setText("");
    }

    /**
     * Search by name
     */
    public void nameSearch() {
        dtm = (DefaultTableModel) tblSemesterList.getModel();
        dtm.setRowCount(0);
        String name = txtSearchByName.getText();
        List<SemesterInfo> result;

        try {
            result = semesterDB.select(name);
            if (result.size() != 0) {
                statusBar.setMessage(result.size() + " match(es) found.", StatusBar.MESSAGE_TYPE);

                addDataToTable(result);
            } else {
                statusBar.setMessage("No match found.", StatusBar.MESSAGE_TYPE);
            }
        } catch (SQLException e) {
            logger.log(Level.WARNING, e.getMessage());
            statusBar.setMessage("Search stopped.", StatusBar.ERROR_TYPE);
        }
    }

    /**
     * Advance feature for semester search
     */
    private void advanceSearch() {
        dtm = (DefaultTableModel) tblSemesterList.getModel();
        dtm.setRowCount(0);
        String name = txtNameDialog.getText();
        int classId = 0;
        try {
            classId = new ClassDb().select()
                    .get(cboClassDialog.getSelectedIndex())
                    .getId();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        List<SemesterInfo> result;

        String strStartDate = dpStartDialog.getText();
        String strEndDate = dpEndDialog.getText();
        Date startDate = (strStartDate.equals("") | strStartDate.equals("   ,  ")) ? null : Helper.toDate(strStartDate, DATE_FORMAT);
        Date endDate = (strEndDate.equals("") | strEndDate.equals(("   ,  "))) ? null : Helper.toDate(strEndDate, DATE_FORMAT);

        try {
            result = semesterDB.select(name, classId, startDate, endDate);

            if (!result.isEmpty()) {
                statusBar.setMessage(result.size() + " match(es) found.", StatusBar.MESSAGE_TYPE);

                addDataToTable(result);
            } else {
                statusBar.setMessage("No match found.", StatusBar.MESSAGE_TYPE);
            }
        } catch (SQLException e) {
            logger.log(Level.WARNING, e.getMessage());
            statusBar.setMessage("Search stopped.", StatusBar.ERROR_TYPE);
        }
        dlgSearch.dispose();
    }

    /**
     * Load all Semesters from database to table when initialize
     */
    private void loadSemestersToTable() {
        classEditor = Helper.addComboBoxToTable(tblSemesterList, cboClassTable, 2);
        JDatePicker datePicker = new JDatePicker();
        datePicker.putClientProperty("JDatePicker.TimePickerUI", SpinnerTimePickerUI.class.getName());
        datePicker.putClientProperty("JDatePicker.showTimePicker", Boolean.TRUE);
        datePicker.setEditable(true);
        tblSemesterList.setDefaultEditor(java.util.Date.class, JDatePicker.createTableCellEditor(datePicker));

        semesterDB = new SemesterDb();
        List<SemesterInfo> semesterList = null;
        try {
            semesterList = semesterDB.select();
        } catch (SQLException ex) {
            logger.log(Level.SEVERE, ex.getMessage());
            statusBar.setMessage("Unable to load data.", StatusBar.WARNING_TYPE);
            statusBar.setMessage("Unable to load data.", StatusBar.WARNING_TYPE);
        }

        addDataToTable(semesterList);
    }

    /**
     * Add data from a list of semesters into table
     *
     * @param result list to add
     */
    private void addDataToTable(List<SemesterInfo> result) {
        ClassDb classDb = new ClassDb();
        classIDs = new ArrayList<Integer>();
        try {
            for (SemesterInfo r : result) {
                String className = classDb.select(r.getClassId()).getName();
                cboClassTable = (JComboBox) classEditor.getComponent();
                cboClassTable.setSelectedItem(className);
                dtm.addRow(new Object[]{
                        r.getId(),
                        r.getName(),
                        className,
                        r.getStartDate(),
                        r.getEndDate(),
                        r.getDescription(),
                        r.isActive()});
                classIDs.add(r.getClassId());
            }
        } catch (SQLException ex) {
            logger.log(Level.SEVERE, null, ex.getSQLState());
            statusBar.setMessage("Unable to load data", StatusBar.ERROR_TYPE);
        }
    }

    /**
     * Load all data to all combobox that is used in this form
     */
    private void loadDataToComboBox() {

        try {
            allClasses = new ClassDb().select();
            List<ClassInfo> activeClass = new ArrayList<ClassInfo>();
            for (ClassInfo ci : allClasses) {
                cboClassDialog.addItem(ci.getName());
                if (ci.isActive()) {

                    cboClass.addItem(ci.getName());
                    cboClassTable.addItem(ci.getName());
                    activeClass.add(ci);
                }
            }
            allClasses = activeClass;
        } catch (SQLException ex) {
            logger.log(Level.SEVERE, ex.getSQLState());
            statusBar.setMessage("Unable to load data.", StatusBar.ERROR_TYPE);
        }
    }

    private void tblSemesterListEventHandler() {
        tblSemesterListCellHandler();
        tblSemesterListRowSorter();
    }

    public void tblSemesterListCellHandler() {
        Action action = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                btnUpdate.setEnabled(true);
                TableCellListener tcl = (TableCellListener) e.getSource();

                int row = tcl.getRow();
                int column = tcl.getColumn();


                //if validate then insert edited record into editingList
                SemesterInfo semesterToUpdate = new SemesterInfo();

                Date startDate = (Date) dtm.getValueAt(row, 3);
                Date endDate = (Date) dtm.getValueAt(row, 4);

                try {
                    cboClassTable = (JComboBox) classEditor.getComponent();

                    semesterToUpdate.setId((Integer) dtm.getValueAt(row, 0));
                    semesterToUpdate.setName((String) dtm.getValueAt(row, 1));
                    semesterToUpdate.setStartDate(
                            new java.sql.Date(startDate.getTime()));
                    semesterToUpdate.setEndDate(
                            new java.sql.Date(endDate.getTime()));
                    if (column == 2)
                        semesterToUpdate.setClassId(
                                allClasses.get(cboClassTable.getSelectedIndex())
                                        .getId()
                        );
                    else
                        semesterToUpdate.setClassId(classIDs.get(row));
                    semesterToUpdate.setDescription((String) dtm.getValueAt(row, 5));
                    semesterToUpdate.setActive((Boolean) dtm.getValueAt(row, 6));

                    editingList.add(semesterToUpdate);
                } catch (NumberFormatException e1) {
                    JOptionPane.showMessageDialog(
                            null,
                            "Wrong format!",
                            "Warning",
                            JOptionPane.ERROR_MESSAGE
                    );
                    dtm.setValueAt(tcl.getOldValue(), row, column);
                } catch (Exception e2) {
                    logger.log(Level.SEVERE, null, e2);
                    statusBar.setMessage("Unknown", StatusBar.ERROR_TYPE);
                }

                //System.out.println("Event fired!");
            }
        };

        new TableCellListener(tblSemesterList, action);
    }

    public void tblSemesterListRowSorter() {
        RowSorter<TableModel> sorter = new TableRowSorter<TableModel>(dtm);
        tblSemesterList.setRowSorter(sorter);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        dlgSearch = new javax.swing.JDialog();
        pnDialog = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtNameDialog = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        cboClassDialog = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        btnSearchDialog = new javax.swing.JButton();
        btnCancelDialog = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        dpStartDialog = new com.standbysoft.component.date.swing.JDatePicker();
        dpStartDialog.setEditable(true);
        dpStartDialog.setEmptySelectionText("<none>");
        dpEndDialog = new com.standbysoft.component.date.swing.JDatePicker();
        dpEndDialog.setEditable(true);
        dpEndDialog.setEmptySelectionText("<none>");
        pnClassInfo = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txaDescription = new javax.swing.JTextArea();
        txtName = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        cboClass = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        dpStart = new com.standbysoft.component.date.swing.JDatePicker();
        dpStart.setEditable(true);
        dpStart.setDateFormat(DATE_FORMAT);
        try {
            dpStart.setEmptySelectionAllowed(false);
        } catch (com.standbysoft.component.date.DateSelectionException e1) {
            e1.printStackTrace();
        }

        dpEnd = new com.standbysoft.component.date.swing.JDatePicker();
        dpEnd.setEditable(true);
        dpEnd.setDateFormat(DATE_FORMAT);
        try {
            dpEnd.setEmptySelectionAllowed(false);
        } catch (com.standbysoft.component.date.DateSelectionException e1) {
            e1.printStackTrace();
        }
        btnAdd = new javax.swing.JButton();
        btnUpdate = new javax.swing.JButton();
        pnClassList = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblSemesterList = new javax.swing.JTable();
        jLabel4 = new javax.swing.JLabel();
        btnRefresh = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        txtSearchByName = new javax.swing.JTextField();
        btnSearchByName = new javax.swing.JButton();
        lblAdvSearch = new javax.swing.JLabel();

        dlgSearch.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        dlgSearch.setTitle("Advance search");
        dlgSearch.setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);

        pnDialog.setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setText("Name:");

        jLabel6.setText("Class:");

        jLabel7.setText("Start date:");

        btnSearchDialog.setText("Search");
        btnSearchDialog.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchDialogActionPerformed(evt);
            }
        });

        btnCancelDialog.setText("Cancel");
        btnCancelDialog.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelDialogActionPerformed(evt);
            }
        });

        jLabel10.setText("End date:");


        javax.swing.GroupLayout pnDialogLayout = new javax.swing.GroupLayout(pnDialog);
        pnDialog.setLayout(pnDialogLayout);
        pnDialogLayout.setHorizontalGroup(
                pnDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(pnDialogLayout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(pnDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(pnDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                                .addComponent(jLabel10, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 67, Short.MAX_VALUE))
                                        .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, 67, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(pnDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(pnDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                .addComponent(txtNameDialog, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
                                                .addComponent(cboClassDialog, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGroup(pnDialogLayout.createSequentialGroup()
                                                        .addComponent(btnSearchDialog)
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(btnCancelDialog)))
                                        .addGroup(pnDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                                .addComponent(dpStartDialog, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(dpEndDialog, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                .addContainerGap(178, Short.MAX_VALUE))
        );
        pnDialogLayout.setVerticalGroup(
                pnDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(pnDialogLayout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(pnDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel1)
                                        .addComponent(txtNameDialog, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(pnDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel6)
                                        .addComponent(cboClassDialog, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(14, 14, 14)
                                .addGroup(pnDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jLabel7)
                                        .addComponent(dpStartDialog, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(pnDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel10)
                                        .addComponent(dpEndDialog, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                                .addGroup(pnDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(btnSearchDialog)
                                        .addComponent(btnCancelDialog))
                                .addContainerGap())
        );

        javax.swing.GroupLayout dlgSearchLayout = new javax.swing.GroupLayout(dlgSearch.getContentPane());
        dlgSearch.getContentPane().setLayout(dlgSearchLayout);
        dlgSearchLayout.setHorizontalGroup(
                dlgSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(pnDialog, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        dlgSearchLayout.setVerticalGroup(
                dlgSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(pnDialog, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        setBackground(new java.awt.Color(255, 255, 255));
        setPreferredSize(new java.awt.Dimension(938, 641));

        pnClassInfo.setBackground(new java.awt.Color(255, 255, 255));
        pnClassInfo.setBorder(javax.swing.BorderFactory.createTitledBorder("Add new semester"));
        pnClassInfo.setPreferredSize(new java.awt.Dimension(452, 219));

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("Name:");
        jLabel2.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel3.setText("Description:");

        txaDescription.setColumns(20);
        txaDescription.setRows(5);
        jScrollPane1.setViewportView(txaDescription);

        jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel8.setText("Class:");

        jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel9.setText("Start date:");

        jLabel11.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel11.setText("End date:");

        javax.swing.GroupLayout pnClassInfoLayout = new javax.swing.GroupLayout(pnClassInfo);
        pnClassInfo.setLayout(pnClassInfoLayout);
        pnClassInfoLayout.setHorizontalGroup(
                pnClassInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(pnClassInfoLayout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(pnClassInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE)
                                        .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(pnClassInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 366, Short.MAX_VALUE)
                                        .addGroup(pnClassInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, pnClassInfoLayout.createSequentialGroup()
                                                        .addComponent(dpStart, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(dpEnd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, pnClassInfoLayout.createSequentialGroup()
                                                        .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addGap(28, 28, 28)
                                                        .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(cboClass, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                .addContainerGap())
        );
        pnClassInfoLayout.setVerticalGroup(
                pnClassInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(pnClassInfoLayout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(pnClassInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel2)
                                        .addComponent(cboClass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel8))
                                .addGap(18, 18, 18)
                                .addGroup(pnClassInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addGroup(pnClassInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(jLabel9)
                                                .addComponent(jLabel11))
                                        .addComponent(dpEnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(dpStart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(pnClassInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel3))
                                .addGap(17, 17, 17))
        );

        btnAdd.setIcon(new ImageIcon(Settings.IMAGES_DIR + "/add-16.png"));
        btnAdd.setText("Add");
        btnAdd.setMaximumSize(new java.awt.Dimension(61, 23));
        btnAdd.setMinimumSize(new java.awt.Dimension(61, 23));
        btnAdd.setPreferredSize(new java.awt.Dimension(61, 23));
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnUpdate.setIcon(new ImageIcon(Settings.IMAGES_DIR + "/update-16.png"));
        btnUpdate.setText("Update");
        btnUpdate.setMaximumSize(new java.awt.Dimension(71, 23));
        btnUpdate.setMinimumSize(new java.awt.Dimension(71, 23));
        btnUpdate.setPreferredSize(new java.awt.Dimension(71, 23));
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });

        pnClassList.setBackground(new java.awt.Color(255, 255, 255));
        pnClassList.setBorder(javax.swing.BorderFactory.createTitledBorder("Semester list"));

        tblSemesterList.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]{

                },
                new String[]{
                        "ID", "Name", "Class", "Start date", "End date", "Description", "Active"
                }
        ) {
            Class[] types = new Class[]{
                    java.lang.Integer.class, java.lang.String.class, java.lang.Object.class, java.util.Date.class, java.util.Date.class, java.lang.String.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean[]{
                    false, true, true, true, true, true, true
            };

            public Class getColumnClass(int columnIndex) {
                return types[columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });
        tblSemesterList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblSemesterListMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tblSemesterList);
        tblSemesterList.getColumnModel().getColumn(0).setPreferredWidth(30);
        tblSemesterList.getColumnModel().getColumn(6).setPreferredWidth(45);

        javax.swing.GroupLayout pnClassListLayout = new javax.swing.GroupLayout(pnClassList);
        pnClassList.setLayout(pnClassListLayout);
        pnClassListLayout.setHorizontalGroup(
                pnClassListLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnClassListLayout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnClassListLayout.setVerticalGroup(
                pnClassListLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(pnClassListLayout.createSequentialGroup()
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 387, Short.MAX_VALUE)
                                .addContainerGap())
        );

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 16));
        jLabel4.setForeground(new java.awt.Color(0, 102, 255));
        jLabel4.setText("MANAGE SEMESTERS");

        btnRefresh.setIcon(new ImageIcon(Settings.IMAGES_DIR + "/refresh-16.gif"));
        btnRefresh.setText("Refresh");
        btnRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRefreshActionPerformed(evt);
            }
        });

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Search for semester"));
        jPanel2.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jPanel2.setMaximumSize(new java.awt.Dimension(452, 219));
        jPanel2.setMinimumSize(new java.awt.Dimension(452, 219));

        jLabel5.setText("Search by name:");

        btnSearchByName.setIcon(new ImageIcon(Settings.IMAGES_DIR + "/search-16.png"));
        btnSearchByName.setText("Search");
        btnSearchByName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchByNameActionPerformed(evt);
            }
        });

        lblAdvSearch.setForeground(new java.awt.Color(0, 51, 255));
        lblAdvSearch.setText("Advance search");
        lblAdvSearch.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        lblAdvSearch.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblAdvSearchMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(jPanel2Layout.createSequentialGroup()
                                                .addComponent(txtSearchByName, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(btnSearchByName))
                                        .addComponent(jLabel5)
                                        .addComponent(lblAdvSearch))
                                .addContainerGap(135, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                .addContainerGap(41, Short.MAX_VALUE)
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(txtSearchByName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(btnSearchByName, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addComponent(lblAdvSearch)
                                .addGap(46, 46, 46))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                                .addContainerGap()
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(jLabel4)
                                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                                        .addComponent(pnClassInfo, javax.swing.GroupLayout.DEFAULT_SIZE, 463, Short.MAX_VALUE))
                                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                .addComponent(pnClassList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                        .addGroup(layout.createSequentialGroup()
                                                .addGap(348, 348, 348)
                                                .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(btnUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(btnRefresh, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addContainerGap())
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGap(24, 24, 24)
                                .addComponent(jLabel4)
                                .addGap(38, 38, 38)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(pnClassInfo, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addComponent(pnClassList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(btnUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(btnRefresh, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(108, 108, 108))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddActionPerformed(ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        doAdd();
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnUpdateActionPerformed(ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
        doUpdate();
    }//GEN-LAST:event_btnUpdateActionPerformed

    private void tblSemesterListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblSemesterListMouseClicked
        btnUpdate.setEnabled(true);
    }//GEN-LAST:event_tblSemesterListMouseClicked

    private void btnRefreshActionPerformed(ActionEvent evt) {//GEN-FIRST:event_btnRefreshActionPerformed
        refresh();

    }//GEN-LAST:event_btnRefreshActionPerformed

    private void btnSearchByNameActionPerformed(ActionEvent evt) {//GEN-FIRST:event_btnSearchByNameActionPerformed
        nameSearch();
    }//GEN-LAST:event_btnSearchByNameActionPerformed

    private void lblAdvSearchMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblAdvSearchMouseClicked
        dlgSearch = new JDialog();
        dlgSearch.setContentPane(pnDialog);
        dlgSearch.setModal(true);
        dlgSearch.pack();
        dlgSearch.setVisible(true);
    }//GEN-LAST:event_lblAdvSearchMouseClicked

    private void btnSearchDialogActionPerformed(ActionEvent evt) {//GEN-FIRST:event_btnSearchDialogActionPerformed
        advanceSearch();
    }//GEN-LAST:event_btnSearchDialogActionPerformed

    private void btnCancelDialogActionPerformed(ActionEvent evt) {//GEN-FIRST:event_btnCancelDialogActionPerformed
        dlgSearch.dispose();

    }//GEN-LAST:event_btnCancelDialogActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnCancelDialog;
    private javax.swing.JButton btnRefresh;
    private javax.swing.JButton btnSearchByName;
    private javax.swing.JButton btnSearchDialog;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JComboBox cboClass;
    private javax.swing.JComboBox cboClassDialog;
    private javax.swing.JDialog dlgSearch;
    private com.standbysoft.component.date.swing.JDatePicker dpEnd;
    private com.standbysoft.component.date.swing.JDatePicker dpEndDialog;
    private com.standbysoft.component.date.swing.JDatePicker dpStart;
    private com.standbysoft.component.date.swing.JDatePicker dpStartDialog;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblAdvSearch;
    private javax.swing.JPanel pnClassInfo;
    private javax.swing.JPanel pnClassList;
    private javax.swing.JPanel pnDialog;
    private javax.swing.JTable tblSemesterList;
    private javax.swing.JTextArea txaDescription;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txtNameDialog;
    private javax.swing.JTextField txtSearchByName;
    // End of variables declaration//GEN-END:variables

}
